package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品接口
 */
@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags="菜品接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("添加菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {

        log.info("添加菜品：{}", dishDTO);
        dishService.addDish(dishDTO);
        //删除缓存
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }

    @ApiOperation("分页查询菜品")
    @GetMapping("/page")
    public Result<PageResult> pageQuery( DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品：{}", dishPageQueryDTO);
        PageResult pageResult=dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     * @param ids，通过Query方式获取集合参数，必须使用@RequestParam注解
     * @return
     */
    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("删除菜品：{}", ids);
        dishService.deleteBatch(ids);
        //删除缓存
        cleanCache("dish_*");

        return Result.success();
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> queryById(@PathVariable Long id){
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO=dishService.queryById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {

        log.info("修改菜品：{}", dishDTO);
        dishService.update(dishDTO);
        //删除缓存
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用或停用菜品")
    private Result startOrStop(Long id, @PathVariable Integer status) {
        dishService.startOrStop(id, status);
        //删除缓存
        cleanCache("dish_*");

        return Result.success();
    }

    private void cleanCache(String pattern) {
        redisTemplate.keys(pattern).forEach(key->{
            redisTemplate.delete(key);
        });
    }




}
